package com.bw.task.user.right;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.library.mvp.view.BaseActivity;
import com.bw.task.R;
import com.bw.task.bean.ImgBean;
import com.bw.task.bean.MoneyBean;
import com.bw.task.bean.PopupWindioBean;
import com.bw.task.bean.UserBean;
import com.bw.task.db.DaoMaster;
import com.bw.task.db.DaoSession;
import com.bw.task.db.User;
import com.bw.task.db.UserDao;
import com.bw.task.mvp.contern.UserContent;
import com.bw.task.mvp.model.UserModel;
import com.bw.task.mvp.persenter.UserPersenter;
import com.bw.task.user.login.LoginActivity;

import java.util.List;


public  class RegActivity extends BaseActivity<UserPersenter> implements UserContent.mView {

    private EditText username;
    private EditText userpass;
    UserDao userDao;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==100){
                try {
                    Thread.sleep(2000);
                    ToastUtils.showShort("注册成功,进入登录界面");
                    startActivity(new Intent(RegActivity.this, LoginActivity.class));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    public void register(View view) {
        String name = username.getText().toString().trim();
        String pass = userpass.getText().toString().trim();
        mPresenter.register(name,pass);
    }

    @Override
    public int bandLayout() {
        return R.layout.act_register;
    }

    @Override
    public void initData() {
        mPresenter = new UserPersenter(this,new UserModel());
        DaoMaster.DevOpenHelper usera = new DaoMaster.DevOpenHelper(this, "usera");
        SQLiteDatabase writableDatabase = usera.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(writableDatabase);
        DaoSession daoSession = daoMaster.newSession();

        userDao = daoSession.getUserDao();

    }

    public void initView() {
        username = (EditText) findViewById(R.id.username);
        userpass = (EditText) findViewById(R.id.userpass);
    }

    @Override
    public void UserBean(UserBean userBean) {
        int code = userBean.getCode();
        if (code==0){

            LogUtils.i(code+"注册成功");
            handler.sendEmptyMessage(100);



        }


    }

    @Override
    public void LogUserBean(UserBean userBean) {

    }

    @Override
    public void MoneyUserBean(MoneyBean userBean) {

    }

    @Override
    public void getImgBean(ImgBean imgBean) {

    }

}